package stack;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author dx
 * @version 1.0
 * @date 2022/3/29 12:56
 * @description:
 */
public class RemoveDup {
    public static void main(String[]args){
        String s = "abbaca";
        System.out.println(removeDuplicates(s));
    }
    public static String removeDuplicates(String s) {
        Deque<Character> stack1 = new LinkedList<Character>();
        int length = s.length();
        for(int i=0;i<length;i++){

            if(stack1.isEmpty()){
                stack1.push(s.charAt(i));
                continue;
            }

            if(stack1.peek()==s.charAt(i)){
                stack1.pop();
                continue;
            }
            stack1.push(s.charAt(i));
        }
        StringBuilder sb = new StringBuilder(stack1.size());
        int len = stack1.size();
        for(int i =0;i<len;i++){
           sb.append(stack1.removeLast());

        }
        return sb.toString();
    }
}
